package com.ruantu.clothing.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruantu.clothing.entity.RolePer;

import com.ruantu.clothing.service.RolePerService;

@Controller
public class RolePerController {


    @Autowired
    private RolePerService rpService;

    @RequestMapping("/addRolePer")
    @ResponseBody
    public int addUserRole(String rp) {

        List<RolePer> list = new ArrayList<RolePer>();
        rp = rp.substring(0, rp.length() - 1) + "]";
        JSONArray obj = JSONArray.parseArray(rp);// 解析String 成JSONARRy
        for (int i = 0; i < obj.size(); i++) {
            JSONObject jo = (JSONObject) obj.get(i);
            list.add(new RolePer(Integer.parseInt(jo.get("roleid").toString()),
                    Integer.parseInt(jo.get("perid").toString())));
        }

        return rpService.tiANRolePer(list);
    }

    @RequestMapping("/delRolePer")
    @ResponseBody
    public int delUserRole(int roleid) {
        return rpService.shanRolePer(roleid);
    }

    @RequestMapping("/findPermission")
    @ResponseBody
    public List<Integer> findRoleId(int roleid) {
        return rpService.getAllPermissionId(roleid);
    }


}
